#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 28 09:49:30 2019

@author: ubuntu
"""

import numpy as np
import pickle as pkl
import pandas as pd
import os


pwd = os.getcwd()
[flow,relate_cross,crossName,target_cross,pred_cross,similar_cross,roadNet,submitList,data_tr]=pkl.load(open(pwd+"/data.pkl",'rb'))

# --------------------------------- predict pred_cross -----------------------------------------
# RW: random walk, use the latest flow as prediction

w = np.ones([6,1])
alpha = 0.7
for k in range(6):
    w[k] = alpha**k
w = w / sum(w)

# predict by random-walk
def RW_pred(flow_mnt,w):
    return np.tile(np.dot(flow_mnt, w),6)

tmp_day = list(range(7))
tmp_minute = list(range(6))
testTime = {7,9,11,14,16,18} # No of time_range
for cross in pred_cross:    
    count = 0
    timeNo = pd.DataFrame(np.zeros([1,1]))
    flow_day = pd.DataFrame(np.zeros([1,7]))
    flow_minute = pd.DataFrame(np.zeros([1,6]))
    # flow_y = pd.DataFrame(np.zeros([1,6])) --> pd.DataFrame(np.zeros([6,1]))
    for k in range(5478,len(flow)):
        time_ = flow.index[k]
        time_tmp = int( int(time_.hour)*100 + int(time_.minute) )
        if time_.day >= 20 and time_.day <= 23 and time_.minute == 30 and time_.hour in testTime:
            print(cross,': ',time_)
            timeNo.loc[count] = time_.hour*2 + (1 if time_.minute>=30 else 0) - 15
            for ii in range(1,8):
                #print(k-288*ii)
                tmp_day[ii-1] = flow.iloc[k-288*ii][cross]
            flow_day.loc[count] = tmp_day
            for ii in range(1,7):
                tmp_minute[ii-1] = flow.iloc[k-ii][cross]
            flow_minute.loc[count] = tmp_minute
            #count = count + 1
            #input_day = flow_day.values
            input_mnt = flow_minute.values
            flow_y = RW_pred(input_mnt,w)
            pred_rng = pd.date_range(start=time_, periods=6, freq = '5min')
            #flow_y = pd.DataFrame(np.transpose(flow_y.values), index=pred_rng, columns=[cross])
            flow.loc[pred_rng,cross] = flow_y.reshape(-1)

pkl.dump(flow,open(pwd+"/result/rw_result.pkl",'wb'))       

